from urllib.parse import urljoin

import scrapy
from scrapy import Request

from ..items import BeiBusItem


class BeibusSpider(scrapy.Spider):
    name = "beiBus"
    allowed_domains = ["beijing.8684.cn"]
    start_urls = ["http://beijing.8684.cn"]

    # 获得并构建一级网页URL
    def start_requests(self):
        for page in range(9):
            url = '{url}/list{page}'.format(url=self.start_urls[0], page=(page + 1))
            yield Request(url, callback=self.parse_index)

    # 构建二级网页(公交路线详情页url)
    def parse_index(self, response):
        beijingbus = response.xpath('//*[@class="list clearfix"]//a/@href').extract()

        for href in beijingbus:
            url2 = urljoin(self.start_urls[0], href)

            yield Request(url2, callback=self.parse_detail)

    # 解析详情页信息提取目标信息
    def parse_detail(self, response):
        # 线路名称

        line_name = response.xpath('//*[@class="title"]/span/text()').extract_first()
        # 线路类型
        line_type = response.xpath('//*[@class="title"]/a/text()').extract_first()
        try:
            # 总里程
            mileage = response.xpath('//div[@class="change-info mb20"]/text()').extract_first()
        except:
            mileage = ""
        # 运行时间
        run_time = response.xpath('//*[@class="bus-desc"]/li[1]/text()').extract_first()
        # 参考票价
        ticket = response.xpath('//*[@class="bus-desc"]/li[2]/text()').extract_first()
        # 公交公司
        company = response.xpath('//*[@class="bus-desc"]/li[3]/a/text()').extract()
        # 最后更新
        update_last = response.xpath('//*[@class="bus-desc"]/li[4]/span/text()').extract_first()

        wang_line_list = []
        fan_line_list = []

        fan_line_name = ""

        wang_name = response.xpath('//*[@class="trip"]/text()').extract()[0]

        wang_fan_line_list = response.xpath('//*[@class="bus-lzlist mb15"]//ol//li//a/@aria-label').extract()
        flag = wang_name.split("—")[-1] + "站点"
        index = 0
        for i in range(len(wang_fan_line_list)):
            if wang_fan_line_list[i].split(" ")[-1] == flag:
                index = i + 1
                break
        wang_line_list = wang_fan_line_list[0:index]

        wang_line_name = line_name + "(" + wang_name + ")"

        try:
            fan_name = response.xpath('//*[@class="trip"]/text()').extract()[1]
            fan_line_list = wang_fan_line_list[index:len(wang_fan_line_list)]
            fan_line_name = line_name + "(" + fan_name + ")"
        except:
            fan_line_list = []

        # 公交路线-往(环形线默认为此项)
        wang_info = wang_line_name + "\n"
        # 公交路线-返
        fan_info = fan_line_name + "\n"

        for i in range(len(wang_line_list)):
            if i != (len(wang_line_list) - 1):
                wang_info += wang_line_list[i] + ","
            else:
                wang_info += wang_line_list[i]
        if len(fan_line_list) != 0:
            for i in range(len(fan_line_list)):
                if i != (len(fan_line_list) - 1):
                    fan_info += fan_line_list[i] + ","
                else:
                    fan_info += fan_line_list[i]

        bus_item = BeiBusItem()

        for field in bus_item.fields:
            bus_item[field] = eval(field)
        yield bus_item
